#pragma once
#include <cstring>
#include "Mutex.hpp"


using namespace MutexModule;

namespace CondModule
{
    class Cond
    {
    public:
        Cond()
        {
            int n = pthread_cond_init(&_cond, nullptr);
            if (n != 0)
            {
                std::cerr << "cond init failed: " << strerror(n) << std::endl;
            }
        }

        void Wait(Mutex& mutex)
        {
            int n = pthread_cond_wait(&_cond, mutex.Get());
            if (n != 0)
            {
                std::cerr << "cond wait failed: " << strerror(n) << std::endl;
            }
        }

        void Signal()
        {
            int n = pthread_cond_signal(&_cond);
            if (n != 0)
            {
                std::cerr << "cond signal failed: " << strerror(n) << std::endl;
            }
        }

        void Broadcast()
        {
            int n = pthread_cond_broadcast(&_cond);
            if (n != 0)
            {
                std::cerr << "cond broadcast failed: " << strerror(n) << std::endl;
            }
        }

        ~Cond()
        {
            int n = pthread_cond_destroy(&_cond);
            if (n != 0)
            {
                std::cerr << "cond destroy failed: " << strerror(n) << std::endl;
            }
        }
    private:
        pthread_cond_t _cond;
    };
}